﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LdExpress;
using System.Data;
using MySql.Data.MySqlClient;

public partial class _Default : System.Web.UI.Page
{
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public string strLetter = "letter";
    public string strAlphaBet = "";
    string strChar = "";
    public string spdVOL_group = "";
    public double spdGW_Sum = 0;
    public double spdMEAS_Sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        DataTable dtList;
        int nNumOfPages = 0;
        if (Session["logined_id"] != null)
        {
            if (Session["cusdoc"] != "") nPermissions = int.Parse(Session["cusdoc"].ToString());

        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            BuildTrackingList(strCondition, -1, nPage, out nNumOfPages, out dtList, null, null);
            rptTracking.DataSource = dtList;
            rptTracking.DataBind();
            LoadCusDoc();
            //Get shipping detail total
            if (Request.QueryString["ID"] != null)
            {
                long lcusdocID = long.Parse(Request.QueryString["ID"].ToString());
                spdVOL_group = ShippingDetail.GetGroupCatContType(lcusdocID);
                spdGW_Sum = ShippingDetail.GetTotalGWByDocID(lcusdocID);
                spdMEAS_Sum = ShippingDetail.GetTotalMEASByDocID(lcusdocID);
            }
            if (nPermissions < 3)
                btnSave.Visible = false;
        }


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //  Response.Redirect("CreateCusdoc.aspx?ID=" + cusdocid.ToString());
        CusdocModel obj = new CusdocModel();
        obj.ID = long.Parse(Request.QueryString["ID"].ToString());
        obj.Load(GV.cn, GV.trans);
        obj.ARRIAL_ATTN = txtARRIALATTN.Text.Trim();
        obj.ARRIAL_ACC = Session["logined_user"].ToString();
        obj.ARRIAL_UPDATE = DateTime.Now;
        obj.UpdateArrial(GV.cn, GV.trans);
        Response.Redirect("ArrialReport.aspx?ID=" + Request.QueryString["ID"].ToString());
    }
    private void LoadCusDoc()
    {
        if (Request.QueryString["ID"] != null)
        {
            long cusdocID = long.Parse(Request.QueryString["ID"].ToString());
            CusdocModel obj = new CusdocModel();
            obj.ID = cusdocID;
            obj.Load(GV.cn, GV.trans);
            string strToMess = "";
            if (obj.MBLType > 0)
            {
                strToMess = obj.MBLSHPR;
                lblBillNo.Text = obj.MBLNO;
            }
            else
            {
                strToMess = obj.HBLSHPR;
                lblBillNo.Text = obj.HBLNO;
            }
            if (strToMess.Length > 0 && (strToMess.IndexOf("\n") > 0))
            {
                strToMess = strToMess.Substring(0, strToMess.IndexOf("\n") + 1);
            }
            lblSHPR.Text = strToMess;
            lblUserName.Text = obj.ARRIAL_ACC;
            txtARRIALATTN.Text = obj.ARRIAL_ATTN;
            lblARRIALUPDATE.Text = obj.ARRIAL_UPDATE.ToString("dd MMMM yyyy");
        }

    }
    private void BuildTrackingList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Index");
        //dtList.Columns.Add("Status");
        dtList.Columns.Add("Location");
        dtList.Columns.Add("TrackingDate");
        dtList.Columns.Add("Vssl");
        dtList.Columns.Add("Voyno");

        dtList.Columns.Add("Discharge");
        dtList.Columns.Add("ETA");
        dtList.Columns.Add("ETDAccept");
        dtList.Columns.Add("ETAAccept");
        dtList.Columns.Add("ETDAcceptStatus");
        dtList.Columns.Add("ETAAcceptStatus");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;
        string[] strValues = null;
        if (Request.QueryString["ID"] != null)
        {
            strCondition += " AND CusdocID=" + Request.QueryString["ID"].ToString();

            Tracking.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    string ID = dr["ID"].ToString();
                    string strIndex = count.ToString();

                    //string Status = dr["Status"].ToString();
                    string Location = dr["Location"].ToString();
                    string TrackingDate = "TBA";
                    if (DateTime.Parse(dr["TrackingDate"].ToString()) != DateTime.MinValue)
                        TrackingDate = DateTime.Parse(dr["TrackingDate"].ToString()).ToString("dd/MM/yyyy");
                    string Vssl = dr["Vssl"].ToString();
                    string Voyno = dr["Voyno"].ToString();

                    string Discharge = dr["Discharge"].ToString();
                    string ETA = "TBA";
                    if (DateTime.Parse(dr["ETA"].ToString()) != DateTime.MinValue)
                        ETA = DateTime.Parse(dr["ETA"].ToString()).ToString("dd/MM/yyyy");
                    string ETDAccept = dr["ETDAccept"].ToString();
                    string ETAAccept = dr["ETAAccept"].ToString();

                    string ETDAcceptStatus = ETDAccept == "1" ? "ATD" : "ETD";
                    string ETAAcceptStatus = ETAAccept == "1" ? "ATA" : "ETA";
                    string Action = " <a href=\"javascript:doDeleteTracking('" + Request.QueryString["ID"].ToString() + "','" + ID + "')\">DEL</a>";

                    strValues = new string[] { ID, strIndex, 
                        Location, TrackingDate, Vssl, Voyno,
                        Discharge,
                        ETA,
                        ETDAccept,
                        ETAAccept,
                        ETDAcceptStatus,
                        ETAAcceptStatus,
                        Action };
                    dtList.Rows.Add(strValues);
                }
            }
        }
    }
}